package cn.weiyan.dtx.txmsgdemo.bank2.message;

import cn.weiyan.dtx.txmsgdemo.bank2.model.AccountChangeEvent;
import cn.weiyan.dtx.txmsgdemo.bank2.server.AccountInfoService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author misterWei
 * @create 2020年02月16号:13点01分
 * @mailbox mynameisweiyan@gmail.com
 *
 * 消费该消息 如果事务发起方,到达了这里,那么此消息必须消费,否则就要进行重试.
 */
@Component
@Slf4j
@RocketMQMessageListener(topic = "topic_txmsg",consumerGroup = "consumer_txmsg_group_bank2")
public class ConsumptionMessage implements RocketMQListener<String>{

   @Autowired
   private AccountInfoService accountInfoService;

    @Override
    public void onMessage(String message) {
        AccountChangeEvent accountChangeEvent = JSON.parseObject(message, AccountChangeEvent.class);
        // 测试原因: 需要更改ID
        accountChangeEvent.setAccountNo("2");
        accountInfoService.addAccountInfoBalance(accountChangeEvent);
        log.info("bank2 消费成功该消息!!!");

    }
}
